package com.wang.music.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.music.entity.Singer;
import com.wang.music.service.SingerService;
import com.wang.music.util.PageUtils;
import com.wang.music.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 歌手表 前端控制器
 * </p>
 *
 * @author zifan
 * @since 2021-03-05
 */
@RestController
@RequestMapping("/singer")
public class SingerController {

    @Autowired
    private SingerService singerService;

    /**
     * 添加歌手
     * @param singer
     * @return Map<String, Object>
     */
    @PostMapping("/addSinger")
    public Map<String, Object> addSinger(@RequestBody Singer singer) {
        return singerService.saveSinger(singer);
    }

    /**
     * 查询所有歌手 (新增分页)
     * @return
     */
    @PostMapping("/getAllSinger")
    public Map<String, Object> getAllSinger(@RequestBody PageVO pageVO, String name) {
        Page<Singer> page = PageUtils.toMpPage(pageVO);
        return singerService.getAllSinger(page);
    }

    /**
     * 根据姓名查询歌手(分页)
     */
    @PostMapping("/getSingerByName")
    public Map<String, Object> getSingerByName(@RequestBody Singer singer) {
        return singerService.getSingerByName(singer.getName());
    }

    /**
     * 根据id更新歌手
     */
    @PostMapping("/updateSinger")
    public Map<String, Object> updateSinger(@RequestBody Singer singer) {
        return singerService.updateSinger(singer);
    }

    /**
     * 删除歌手
     */
    @PostMapping("/delSinger")
    public Map<String,Object> delSinger(@RequestBody Singer singer){
        return singerService.delSinger(singer);
    }

    /**
     * 上传歌手头像
     */
    @PostMapping("/changePic")
    public Map<String,Object> changePic(@RequestParam("file") MultipartFile pic) throws IOException {
        return singerService.changePic(pic);
    }

    // ----------------------------------------------------------------------------------------

    /**
     * 以下是前端需要的controller
     */
    /**
     * 获取全部的singer
     *
     */
    @GetMapping("/allSinger")
    public List<Singer> allSinger(){
        return singerService.allSinger();
    }

}

